package com.xbh.management.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nimbusds.jose.JOSEException;
import com.xbh.common.utils.JwtUtil;
import com.xbh.common.utils.JwtUtils;
import com.xbh.management.entity.ManRoleVO;
import com.xbh.management.exception.JwtSignatureVerifyException;
import com.xbh.management.mapper.RoleMenuMapper;
import com.xbh.management.model.ManMenu;
import com.xbh.management.model.ManRole;
import com.xbh.management.model.ManRoleMenu;
import com.xbh.management.utils.JwtUtilManagement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @program: management-center
 * @description:
 * @author: 许宝华
 * @create: 2021-10-05 17:24
 */

@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, ManRoleMenu> implements RoleMenuService {

    @Autowired
    private RoleService roleService;
    /**
     * 角色关联菜单
     */
    @Override
    public int addRoleRelationMenu(List menuIdList, Long roleId) {
        AtomicInteger insert= new AtomicInteger();

        //获取当前角色已关联的菜单
        ManRole role = new ManRole();
        role.setRoleId(roleId);
        ManRoleVO manRoleVO = roleService.queryMenuBuRole(role);
        //当前角色还没有关联菜单
        if(manRoleVO == null){
            menuIdList.stream().forEach( menuId -> {
                ManRoleMenu manRoleMenu = new ManRoleMenu();
                manRoleMenu.setMenuId(Long.parseLong(menuId.toString()));
                manRoleMenu.setRoleId(roleId);
                manRoleMenu.setCreatePerson(JwtUtil.getUsername());
                manRoleMenu.setUpdatePerson(JwtUtil.getUsername());
                insert.set(this.baseMapper.insert(manRoleMenu));
            });
            return insert.get();
        }
        List<ManMenu> oldMenuList = manRoleVO.getMenuList();
        ArrayList<Long> longMenuList = new ArrayList<>();
        for (ManMenu menu : oldMenuList) {
            longMenuList.add(menu.getMenuId());
        }
        //前端传进来的数据大于本身查到的即为 新增关联菜单
        if(menuIdList.size() > oldMenuList.size()){
            //过滤掉已关联的
            List<Long> collect = (List<Long>) menuIdList.stream().filter(item -> !longMenuList.contains(item)).collect(Collectors.toList());
            collect.stream().forEach( menuId -> {
                ManRoleMenu manRoleMenu = new ManRoleMenu();
                manRoleMenu.setMenuId(menuId);
                manRoleMenu.setRoleId(roleId);
                manRoleMenu.setCreatePerson(JwtUtil.getUsername());
                manRoleMenu.setUpdatePerson(JwtUtil.getUsername());
                insert.set(this.baseMapper.insert(manRoleMenu));
            });
            return insert.get();
        //删除关联菜单
        }else{
            List<Long> collect = longMenuList.stream().filter(item -> !menuIdList.contains(item)).collect(Collectors.toList());
            collect.stream().forEach(menuId -> {
                QueryWrapper<ManRoleMenu> wrapper = new QueryWrapper<>();
                wrapper.eq("role_id",roleId);
                wrapper.eq("menu_id",menuId);
                int delete = this.baseMapper.delete(wrapper);
                insert.set(delete);
            });

            return insert.get();
        }

    }
}
